package com.hltx.controller.join;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hltx.pojo.user.User;
import com.hltx.pojo.user.UserLogin;
import com.hltx.service.user.UserManager;
import com.hltx.util.MD5Util;

@Controller
@RequestMapping(value = "/join/")
public class UserInfoController {
	@Resource(name = "userService")
	private UserManager userService;
	
	//@Autowired
	//private PayManager payService;
	
	@RequestMapping(value = "getPoints", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> getPoints(HttpServletResponse response, HttpServletRequest request) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		response.setHeader("Access-Control-Allow-Origin", "*");
		String loginId = request.getParameter("loginId");
		try {
			if(loginId != null){
				String points = userService.getPoints(loginId);
				resMap.put("result", points);
			}else{
				resMap.put("result", "参数不完整");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resMap.put("result", "系统异常");
		}
		return resMap;
	}
	
	@ResponseBody
	@RequestMapping(value = "registe", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> register(HttpServletResponse response,HttpServletRequest request){
		Map<String, Object> resMap = new HashMap<String, Object>();
		response.setHeader("Access-Control-Allow-Origin", "*");
		String userName = request.getParameter("mobile");
		String pasword = request.getParameter("pasword");
		String points = request.getParameter("points");
		String hxId = "hltx"+MD5Util.MD5(userName);
		UserLogin login =new UserLogin();
		User resultUser = null;
		if(userName != null && pasword != null && points!= null){
		login.setPwd(pasword);
		login.setUserName(userName);
		login.setHxId(hxId);
		login.setPoint(points);
			try {
				String loginId =userService.findByPhonea(login);
				if(loginId == null){
					int id = userService.saveUserReg(login,points);
					if(id > 0){
						 resultUser = userService.getUserInfo(id);
						if(resultUser != null){
							try {
								userService.saveM(resultUser,points);
							} catch (Exception e) {
								e.printStackTrace();
								resMap.put("msg",  "error");
							}
							resMap.put("msg", "success");
							resMap.put("result", resultUser.getLoginId());
						}else{
							resMap.put("result", "注册出现问题请及时联系");
						}
					}else{
						resMap.put("msg", "error");
					}
				}else{
					try {
						userService.saveMs(loginId,points,userName);
					} catch (Exception e) {
						e.printStackTrace();
						resMap.put("msg",  "error");
					}
					resMap.put("msg", "success");
					resMap.put("result", loginId);
				}
			} catch (Exception e) {
				e.printStackTrace();
				resMap.put("msg",  "error");
			}
		}else{
			resMap.put("msg", "error");
		}
		return resMap;
	}
}
